package com.example.interfacecall.controller;

import com.alibaba.fastjson.JSON;
import com.example.interfacecall.domain.ClaimsNoticeDto;
import com.example.interfacecall.domain.GiveUpNoticeDto;
import com.example.interfacecall.domain.Result;
import com.example.interfacecall.exception.BusinessException;
import com.example.interfacecall.util.HttpUtils;
import com.example.interfacecall.util.ResultWrapper;
import com.example.interfacecall.util.ResultWrapperMapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;

@Validated
@RestController
public class GiveUpNoticeController {
    @Value("${interface.url}")
    private String giveUpNoticeUrl;

    @GetMapping("/giveUpNoticePage")
    public ModelAndView giveUpNoticePage() {
        return new ModelAndView("giveupnotice");
    }

    @PostMapping("/giveupnotice")
    public ResultWrapper<Result> giveupnotice(@Valid @RequestBody @ModelAttribute(value = "giveupnotice") GiveUpNoticeDto giveUpNoticeDto) {
        try{
            String interfaceName = "giveupnotice";
            String url = giveUpNoticeUrl +"/"+ interfaceName;
            String param = JSON.toJSONString(giveUpNoticeDto);
            String resultOut = HttpUtils.sendPost(url, param);
            Result result = JSON.parseObject(resultOut, Result.class);
            System.out.println(result);
            return ResultWrapperMapper.ok(result);
        } catch (BusinessException e){
            return ResultWrapperMapper.wrap(e);
        }
    }
}
